import { createStore } from "vuex";
import createPersistedState from "vuex-persistedstate";
const ports = [11111, 11357, 11358, 11359, 13601];
const getTime = () => {
    return new Date().getTime().toString();
};
export default createStore({
    state: {
        // 当前设备
        currDev: "",
        // 连接过设备列表
        deviceList: {},
        // 操作模式
        mode: "KeysMode",
        socket: null,
        loading: false,
        isConnect: false,
    },
    mutations: {
        setSocket(state, data) {
            state.socket = data;
            state.isConnect = true;
        },
        setDevice(state, data) {
            state.deviceList[data.body.ip] = data;
        },
        setCurrDev(state, data) {
            state.currDev = data;
        },
        close(state) {
            state.loading = false
            state.socket = null
            state.isConnect = false
        }
    },
    getters: {
        getCurrDev: state => {
            return state.deviceList[state.currDev]
        }
    },
    actions: {
        connect({ commit, state }, ip) {
            state.loading = true;
            for (let i = 0, leng = ports.length; i < leng; i++) {
                if (state.socket) {
                    break
                }
                const port = ports[i];
                const ws = new WebSocket(`ws://${ip.value}:${port}`);
                ws.onopen = () => {
                    ws.send(
                        `{"head":{"cmd":"connect","from":"weapp","id":"${getTime()}"},"body":{"version":1}}`
                    );
                    commit("setSocket", ws);
                    ws.onmessage = (val) => {
                        const data = JSON.parse(val.data);
                        console.log(data)
                        if (data.body.ip) {
                            commit("setDevice", data);
                            commit("setCurrDev", data.body.ip);
                        }
                        state.loading = false;
                        return
                    };
                    ws.onerror = () => {
                        commit('close')
                    }
                    ws.onclose = () => {
                        commit('close')
                    }
                };

            }
        },
        autoConnect({ getters, commit }, info) {
            const dev = info || getters.getCurrDev
            if (dev) {
                console.log(dev)
                const ws = new WebSocket(`ws://${dev.body.ip}:${dev.body.w_port}`);
                ws.onopen = () => {
                    ws.send(
                        `{"head":{"cmd":"connect","from":"weapp","id":"${getTime()}"},"body":{"version":1}}`
                    );
                    ws.onmessage = (val) => {
                        const data = JSON.parse(val.data);
                        if (data.body.ip) {
                            commit("setDevice", data);
                            commit("setCurrDev", data.body.ip);
                        }
                        return
                    };
                    commit("setSocket", ws);
                    ws.onerror = () => {
                        commit('close')
                    }
                    ws.onclose = () => {
                        commit('close')
                    }
                };

            }
            console.log(getters)
        },
        control({ state }, data) {
            const ws = state.socket;
            if (!ws) {
                return;
            }
            const info = {
                body: {},
                head: { cmd: "voice_command", from: "weapp", id: `${getTime()}` },
            };
            switch (data) {
                case "top":
                    info.body["command"] = "rmctrl_19";
                    break;
                case "down":
                    info.body["command"] = "rmctrl_20";
                    break;
                case "left":
                    info.body["command"] = "rmctrl_21";
                    break;
                case "right":
                    info.body["command"] = "rmctrl_22";
                    break;
                case "ok":
                    info.body["command"] = "rmctrl_23";
                    break;
                case "menu":
                    info.body["command"] = "rmctrl_82";
                    break;
                case "home":
                    info.body["command"] = "rmctrl_3";
                    break;
                case "return":
                    info.body["command"] = "rmctrl_4";
                    break;
                case "volume+":
                    info.body["command"] = "rmctrl_24";
                    break;
                case "volume-":
                    info.body["command"] = "rmctrl_25";
                    break;
                case 'voice_start':
                    info.head.cmd = "voice_start"
                    break;
                case 'voice_stop':
                    info.head.cmd = "voice_stop"
                    break;
                default:
                    info.body["command"] = data;
            }
            ws.send(JSON.stringify(info));
        },
    },
    modules: {},
    plugins: [createPersistedState()],
});